package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        //1执行分页查询
        //1.1设置分页参数
        Page<Setmeal> pageInfo1 = new Page<>(page,pageSize);
        //1.2设置查询条件
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByAsc("update_time");
        setmealService.page(pageInfo1,queryWrapper);

        //2.进行数据拷贝
        //2.1拷贝第一部分(total)
        //创建一个SetmealDto的page对象
        Page<SetmealDto> pageInfo2 = new Page<>();
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

        //2.2拷贝第二部分(records)
        List<SetmealDto> SetmealDtolist = new ArrayList<>();
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            //创建setmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //将setmeal拷贝给setmealDto
            BeanUtils.copyProperties(setmeal,setmealDto);
            //TODO 获取分类名称
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            SetmealDtolist.add(setmealDto);
        }
        pageInfo2.setRecords(SetmealDtolist);
        return R.success(pageInfo2);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> remove(Long [] ids){
        if (ids.length>0){
            setmealService.removeWithDish(Arrays.asList(ids));
            return R.success("删除成功");
        }
        return R.error("没有勾选数据,删除失败");
    }

    /**
     * 起售,停售
     * @return
     */
    @PostMapping("/status/{status}")
    public R sell(@PathVariable("status") Setmeal setmeal,Long [] ids){
        return null;
    }


    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //创建查询条件
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",setmeal.getCategoryId());
        queryWrapper.orderByDesc("update_time");
        //调用方法获取list集合
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
